package com.google.code.jtasks.model;

import com.google.code.jtasks.manager.cache.TaskEnumsRegistry;

/**
 * Models the relative priority/importance of tasks. The {@link Priority} {@link #getLabel()} values are
 * generally specific to the task system, but the {@link #getLevel()} values should be comparable across
 * systems.
 */
public class Priority
{
	private final int level;
	private final String label;

	/**
	 * <p>
	 * Constructs a new {@link Priority} instance.
	 * </p>
	 * <p>
	 * Generally, users should avoid creating new {@link Priority} instances themselves and should instead
	 * retrieve them from the {@link TaskEnumsRegistry} for the task system in use.
	 * </p>
	 * 
	 * @param level
	 *            a positive <code>int</code> that is used to represent the relative importance/urgency of
	 *            this {@link Priority} versus others, where lower values are considered to be more
	 *            important/urgent than higher-valued {@link Priority}s
	 * @param label
	 *            the label used by the task system to identify the {@link Priority}
	 */
	public Priority(int level, String label)
	{
		if (label == null)
			throw new IllegalArgumentException("null label not allowed");

		this.level = level;
		this.label = label;
	}

	/**
	 * Returns a positive <code>int</code> that is used to represent the relative importance/urgency of this
	 * {@link Priority} versus others, where lower values are considered to be more important/urgent than
	 * higher-valued {@link Priority}s.
	 * 
	 * @return a positive <code>int</code> that is used to represent the relative importance/urgency of this
	 *         {@link Priority} versus others, where lower values are considered to be more important/urgent
	 *         than higher-valued {@link Priority}s
	 */
	public int getLevel()
	{
		return level;
	}

	/**
	 * Returns the label used by the task system to identify the {@link Priority}.
	 * 
	 * @return the label used by the task system to identify the {@link Priority}
	 */
	public String getLabel()
	{
		return label;
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj)
	{
		if (obj == null || !(obj instanceof Priority))
			return super.equals(obj);
		Priority other = (Priority) obj;

		if (level != other.level)
			return false;
		if (!label.equals(other.label))
			return false;

		return true;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode()
	{
		int hash = level;
		hash ^= label.hashCode();

		return hash;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		StringBuilder debugText = new StringBuilder();
		debugText.append(this.getClass().getSimpleName());
		debugText.append("[");
		debugText.append("level=");
		debugText.append(level);
		debugText.append(",label=");
		debugText.append(label);
		debugText.append("]");
		return debugText.toString();
	}
}
